<script setup lang="ts">
import { getKnowledgePage } from '@/services/consult'
import KnowledgeCard from './KnowledgeCard.vue'
import type {
  KnowledgeList,
  KnowledgeParams,
  KnowledgeType
} from '@/types/consult'
import { ref } from 'vue'
// 加载状态中
const loading = ref(false)
// 是否完全加载完
const finished = ref(false)
// 数据列表
const list = ref<KnowledgeList>([])

// 显示的类型 KonwledgeType 'like' | 'recommend' | 'fatReduction' | 'food'
const props = defineProps<{
  type: KnowledgeType
}>()
//传递的类型
const params = ref<KnowledgeParams>({
  type: props.type, //传递的是 'like' | 'recommend' | 'fatReduction' | 'food'
  current: 1, // 传递多少页
  pageSize: 10 // 一页多少条数据
})
// 滚动到最底部
const onLoad = async () => {
  const res = await getKnowledgePage(params.value)
  console.log(res)
  list.value.push(...res.data.rows)
  // 判断 如果要传递的页树>= 数据返回多少页
  if (params.value.current >= res.data.pageTotal) {
    // 则完全加载完毕
    finished.value = true
  } else {
    // 否则就页数++ 下一个页
    params.value.current++
  }
  // 加载完毕
  loading.value = false
}
</script>

<template>
  <div class="knowledge-list">
    <!-- loading 页面加载中(控制加载中效果)  finished 加载完毕 finished-text加载完毕显示-->
    <van-list
      v-model:loading="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <knowledge-card
        v-for="item in list"
        :key="item.id"
        :item="item"
      ></knowledge-card>
    </van-list>
  </div>
</template>

<style lang="scss" scoped>
.knowledge-list {
  padding: 0 15px;
}
</style>
